local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local levelAudio = audio.loadSound("levelSound.mp3")
local levelChannel

----------------------------------------------------------------------------------
-- 
--      NOTE:
--      
--      Code outside of listener functions (below) will only be executed once,
--      unless storyboard.removeScene() is called.
-- 
---------------------------------------------------------------------------------


-- local forward references should go here --


---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- Called when the scene's view does not exist:
function scene:createScene( event )
        local group = self.view
		
        -----------------------------------------------------------------------------

        --      CREATE display objects and add them to 'group' here.
        --      Example use-case: Restore 'group' from previously saved state.

        -----------------------------------------------------------------------------
		
		print(display.contentWidth)
		
		background = display.newImageRect("creditsbg.png",BASE_SCREEN_WIDTH,BASE_SCREEN_HEIGHT)
		background.x = display.contentWidth / 2
		background.y = display.contentHeight / 2  
				
		buttonUpVisit = display.newImageRect("visitBntUp.png",235,69)
		buttonDownVisit = display.newImageRect("visitBntDown.png",230,60)
		
		menuLvButtonUp = display.newImageRect("leMenuBntUp.png",79,77)
		menuLvButtonDown = display.newImageRect("leMenuBntDown.png",79,77)
		
		buttonUpVisit.y = 350
		buttonUpVisit.x = display.contentWidth/2
		buttonDownVisit.y = 350
		buttonDownVisit.x = display.contentWidth/2
		buttonDownVisit.isVisible = false
		
		menuLvButtonDown.isVisible = false
		menuLvButtonUp.x = 43
		menuLvButtonUp.y = display.contentHeight/2 + 195
		menuLvButtonDown.x = 43
		menuLvButtonDown.y = display.contentHeight/2 + 195
		
		group:insert(background)
		group:insert(buttonUpVisit)
		group:insert(buttonDownVisit)
		group:insert(menuLvButtonUp)
		group:insert(menuLvButtonDown)
		
			
		--[[
			Page link		
		]]--
				
		local function visitListener(event) 			
			local function visitLink()
				print("Goto Venturus Page")
				buttonDownVisit.isVisible = false
				buttonUpVisit.isVisible = true
				system.openURL("http://www.venturus.org.br/index.php/venturus-educacao")
			end			
			if (event.phase == "began") then
				buttonDownVisit.isVisible = true
				buttonUpVisit.isVisible = false
				timer.performWithDelay(100,visitLink,1)	
			elseif (event.phase == "ended") then
				--buttonDownNewGame.isVisible = false
				--buttonUpNewGame.isVisible = true
			end						
			return true
		end 
		buttonUpVisit:addEventListener( "touch", visitListener )
		
	
		local function menuBntTouch(touchEvent)
			local function menuGame()
				print("goto menu")
				storyboard.purgeScene("Credits")
				storyboard.gotoScene("Menu",{time=200,effect="fade"})												
			end						
			if (touchEvent.phase == "began") then
				menuLvButtonDown.isVisible = true
			else
				menuGame()
				menuLvButtonDown.isVisible = false
			end			
			return true
		end 
		menuLvButtonUp:addEventListener( "touch", menuBntTouch )
		
end


-- Called BEFORE scene has moved onscreen:
function scene:willEnterScene( event )
        local group = self.view
		levelChannel = audio.play(levelAudio,{loops = -1})
        -----------------------------------------------------------------------------

        --      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end


-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
        local group = self.view
		
        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. start timers, load audio, start listeners, etc.)

        -----------------------------------------------------------------------------

end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
        local group = self.view
		audio.stop(levelChannel)
        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

        -----------------------------------------------------------------------------

end


-- Called AFTER scene has finished moving offscreen:
function scene:didExitScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. remove listeners, widgets, save state, etc.)

        -----------------------------------------------------------------------------

end


-- Called if/when overlay scene is displayed via storyboard.showOverlay()
function scene:overlayBegan( event )
        local group = self.view
        local overlay_name = event.sceneName  -- name of the overlay scene

        -----------------------------------------------------------------------------

        --      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end


-- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()
function scene:overlayEnded( event )
        local group = self.view
        local overlay_name = event.sceneName  -- name of the overlay scene

        -----------------------------------------------------------------------------

        --      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end



---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "willEnterScene" event is dispatched before scene transition begins
scene:addEventListener( "willEnterScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "didExitScene" event is dispatched after scene has finished transitioning out
scene:addEventListener( "didExitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-- "overlayBegan" event is dispatched when an overlay scene is shown
scene:addEventListener( "overlayBegan", scene )

-- "overlayEnded" event is dispatched when an overlay scene is hidden/removed
scene:addEventListener( "overlayEnded", scene )

---------------------------------------------------------------------------------

return scene